<?php
//微信用户相关

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\DateModel;
use App\Traits\BaseHttp;

class DateController extends BaseController
{
    use BaseHttp;
    public function syncHoliday (Request $request)
    {
        $year = $request->get('year');
        $yearCount = DateModel::where('year', $year)
            ->count();
        if ($yearCount > 0) {
            echo '当前年份已存在数据，请清空后再导入';die;
        }
        date_default_timezone_set('UTC');

        // 获取2024年的起始日期和结束日期
        $start_date = new \DateTime($year . '-01-01');
        $end_date = new \DateTime($year . '-12-31');

        // 创建一个数组来存储周末日期
        $weekends = array();

        // 循环遍历每一天，检查是否为周末
        $current_date = clone $start_date;
        while ($current_date <= $end_date) {
            $day_of_week = $current_date->format('N'); // 返回星期几，1代表周一，7代表周日
            if ($day_of_week == 6 || $day_of_week == 7) { // 周六或周日
                $weekends[$current_date->format('Y-m-d')] = 2;
            }
            $current_date->modify('+1 day'); // 增加一天
        }
        // 节假日接口(不知道为什么)
        $holidayUrl = 'https://timor.tech/api/holiday/year/' . $year;
        $holidayJson = '{"code":0,"holiday":{"01-01":{"holiday":true,"name":"元旦","wage":3,"date":"2025-01-01","rest":39},"01-26":{"holiday":false,"name":"春节前补班","wage":1,"after":false,"target":"春节","date":"2025-01-26","rest":24},"01-28":{"holiday":true,"name":"除夕","wage":2,"date":"2025-01-28","rest":26},"01-29":{"holiday":true,"name":"初一","wage":3,"date":"2025-01-29","rest":1},"01-30":{"holiday":true,"name":"初二","wage":3,"date":"2025-01-30","rest":1},"01-31":{"holiday":true,"name":"初三","wage":3,"date":"2025-01-31","rest":1},"02-01":{"holiday":true,"name":"初四","wage":2,"date":"2025-02-01","rest":1},"02-02":{"holiday":true,"name":"初五","wage":2,"date":"2025-02-02","rest":1},"02-03":{"holiday":true,"name":"初六","wage":2,"date":"2025-02-03","rest":1},"02-04":{"holiday":true,"name":"初七","wage":2,"date":"2025-02-04","rest":1},"02-08":{"holiday":false,"name":"春节后补班","wage":1,"target":"春节","after":true,"date":"2025-02-08","rest":1},"04-04":{"holiday":true,"name":"清明节","wage":3,"date":"2025-04-04","rest":37},"04-05":{"holiday":true,"name":"清明节","wage":2,"date":"2025-04-05","rest":1},"04-06":{"holiday":true,"name":"清明节","wage":2,"date":"2025-04-06","rest":1},"04-27":{"holiday":false,"name":"劳动节前补班","wage":1,"target":"劳动节","after":false,"date":"2025-04-27","rest":5},"05-01":{"holiday":true,"name":"劳动节","wage":3,"date":"2025-05-01","rest":9},"05-02":{"holiday":true,"name":"劳动节","wage":2,"date":"2025-05-02","rest":1},"05-03":{"holiday":true,"name":"劳动节","wage":3,"date":"2025-05-03","rest":1},"05-04":{"holiday":true,"name":"劳动节","wage":3,"date":"2025-05-04","rest":1},"05-05":{"holiday":true,"name":"劳动节","wage":3,"date":"2025-05-05","rest":1},"05-31":{"holiday":true,"name":"端午节","wage":3,"date":"2025-05-31","rest":26},"06-01":{"holiday":true,"name":"端午节","wage":2,"date":"2025-06-01","rest":1},"06-02":{"holiday":true,"name":"端午节","wage":2,"date":"2025-06-02","rest":1},"09-28":{"holiday":false,"name":"国庆节前补班","after":false,"wage":1,"target":"国庆节","date":"2025-09-28","rest":27},"10-01":{"holiday":true,"name":"国庆节","wage":3,"date":"2025-10-01","rest":30},"10-02":{"holiday":true,"name":"国庆节","wage":3,"date":"2025-10-02","rest":1},"10-03":{"holiday":true,"name":"国庆节","wage":3,"date":"2025-10-03","rest":1},"10-04":{"holiday":true,"name":"国庆节","wage":2,"date":"2025-10-04","rest":1},"10-05":{"holiday":true,"name":"国庆节","wage":2,"date":"2025-10-05","rest":1},"10-06":{"holiday":true,"name":"中秋节","wage":2,"date":"2025-10-06","rest":1},"10-07":{"holiday":true,"name":"国庆节","wage":2,"date":"2025-10-07","rest":1},"10-08":{"holiday":true,"name":"国庆节","wage":2,"date":"2025-10-08","rest":1},"10-11":{"holiday":false,"after":true,"wage":1,"name":"国庆节后补班","target":"国庆节","date":"2025-10-11"}}}';
        $holidaies = json_decode($holidayJson, true);
        $otherHolidaies = $holidaies['holiday'];
        foreach ($otherHolidaies as $otherHoliday) {
            $weekends[$otherHoliday['date']] = 2;
        }

        // 设置2024年起始日期和结束日期
        $start_date = new \DateTime($year . '-01-01');
//        $end_date = new \DateTime($year . '-12-31');
        $end_date = new \DateTime($year + 1 . '-01-01');

        // 创建日期范围
        $date_interval = new \DateInterval('P1D');
        $date_range = new \DatePeriod($start_date, $date_interval, $end_date);
        // 遍历日期范围并输出日期
        foreach ($date_range as $date) {
            $dateString = $date->format('Y-m-d');
            $now = date('Y-m-d');
            $date = [
                'date' => $dateString,
                'year' => $year,
                'is_open' => empty($weekends[$dateString]) ? 1 : $weekends[$dateString],
                'created_at' => $now,
                'updated_at' => $now,
            ];
            $dates[] = $date;
        }
        $res = DateModel::insert($dates);
        if ($res) {
            echo '写入数据库成功';
        } else {
            echo '写入数据库失败';
        }
    }
}
